import {DiarySchame} from './DiarySchame';
import Realm from 'realm';
let angryMood = require('./image/angry.png');
let happyMood = require('./image/happy.png');
let peaceMood = require('./image/peace.png');
let sadMood = require('./image/sad.png');
let miseryMood = require('./image/misery.png');
// 根据提供的表初始化 Realm，可同时往数组中放入多个表
// 初始化realm
let realm = new Realm({schema: [DiarySchame]});
export default class RealmHelper {
  static getAllDiary() {
    let res = realm.objects('Diary').sorted('createTime', true);
    return res;
  }
  static saveDiary(newId, newDiaryMood, newDiaryBody, newDiaryTitle) {
    new Promise((resolve, reject) => {
      try {
        realm.write(() => {
          realm.create('Diary', {
            uid: newId,
            title: newDiaryTitle,
            body: newDiaryBody,
            mood: newDiaryMood,
          });
          resolve('ok');
        });
      } catch (e) {
        console.log('write error: ', e);
        resolve('error');
      }
    });
  }
  static deleteDiary(id) {
    try {
      realm.write(() => {
        realm.delete(realm.objects('Diary').filtered('uid="' + id + '"'));
      });
    } catch (e) {
      console.log('delete err:', e);
    }
  }
}
